#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 22 16:57:19 2022

@author: mac
"""

import PySimpleGUI as sg
from main import ActivityGenerator
from main import FragmentGenerator
from main import observablefield
from main import UICache

class MainUI(object):
    
    def __init__(self):
        self.uiCache = UICache("ui.txt")
        pass
    
    def show(self):
        readCache = self.uiCache.load()
        default_name = ""
        default_author = ""
        default_model = ""
        try:
            default_name = readCache["name"]
            default_author = readCache["author"]
            default_model = readCache["model"]
        except Exception as e:
            pass
        
        sg.theme('DarkAmber')   # 设置当前主题
        # 界面布局，将会按照列表顺序从上往下依次排列，二级列表中，从左往右依此排列
        self.layout = [  [sg.Text('name'),sg.InputText(default_text=default_name,key='name')],
                    [sg.Text('author'), sg.InputText(default_text=default_author,key='author')],
                    [sg.Text('model'), sg.InputText(default_text=default_model,key='model')],
                    [sg.Checkbox('support_navigation', key = 'support_navigation')],
                    [sg.Radio('Fragment', "RadioDemo", default=True, size=(10,1), k='rd_fragment'), sg.Radio('Activity', "RadioDemo", default=False, size=(10,1), k='rd_activity')],
                    [sg.Button('Ok'), sg.Button('Cancel')] ]
         

        # 创造窗口
        self.window = sg.Window('代码生成神器', self.layout)

        self.name = observablefield(self.set_name, self.get_name)
        self.author = observablefield(self.set_author, self.get_author)
        self.model = observablefield(self.set_model, self.get_model)
        self.support_navigation = observablefield(self.set_support_navigation, self.get_support_navigation)
        self.rd_fragment = observablefield(self.set_rd_fragment, self.get_rd_fragment)
        self.rd_activity = observablefield(self.set_rd_activity, self.get_rd_activity)

        # 事件循环并获取输入值
        while True:
            event, values = self.window.read()
            if event in (None, 'Cancel'):   # 如果用户关闭窗口或点击`Cancel`
                break
            
            if self.is_valid():
                obj = {}
                obj["name"] = self.name.get()
                obj["author"] = self.author.get()
                obj["model"] = self.model.get()
                self.uiCache.write(obj)
                
                if self.rd_fragment.get():
                    FragmentGenerator(name=self.name.get(), author=self.author.get(), model=self.model.get())
                else:
                    ActivityGenerator(name=self.name.get(), support_navigation=self.support_navigation.get(), author=self.author.get(), model=self.model.get())
                sg.popup_ok('已生成')
            else:
                sg.popup_ok('请输入参数')
         
        self.window.close()
        
    def is_valid(self):
        if self.name.get() == "":
            return False
        if self.author.get() == "":
            return False
        if self.model.get() == "":
            return False
        
        return True

    # 定义 databinding 的 setter, getter 回调
    def set_name(self,str):
        self.window["name"].update(value=str)
        
    def get_name(self):
        return self.window["name"].get()
    
    def set_author(self,str):
        self.window["author"].update(value=str)
        
    def get_author(self):
        return self.window["author"].get()
    
    def set_model(self,str):
        self.window["model"].update(value=str)
        
    def get_model(self):
        return self.window["model"].get()
    
    def set_support_navigation(self,str):
        self.window["support_navigation"].update(value=str)
        
    def get_support_navigation(self):
        return self.window["support_navigation"].get()
    
    def set_rd_fragment(self,str):
        self.window["rd_fragment"].update(value=str)
        
    def get_rd_fragment(self):
        return self.window["rd_fragment"].get()
    
    def set_rd_activity(self,str):
        self.window["rd_activity"].update(value=str)
        
    def get_rd_activity(self):
        return self.window["rd_activity"].get()

MainUI().show()
